package com.ge.modules.sys.controller;

import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.entity.TbCommodityEntity;
import com.ge.modules.sys.service.SysCommodityService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 商品
 * 
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-04-11 17:29:31
 */
@RestController
@RequestMapping("/sys/commodity")
public class SysCommodityController {
	@Autowired
	private SysCommodityService sysCommodityService;
	
	/**
	 * 显示商品列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sysCommodity:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<TbCommodityEntity> tbCommodityEntityList = sysCommodityService.queryList(query);
		int total = sysCommodityService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(tbCommodityEntityList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sysCommodity:info")
	public R info(@PathVariable("id") Integer id){
		TbCommodityEntity tbCommodityEntity = sysCommodityService.queryId(id);
		
		return R.ok().put("commodity", tbCommodityEntity);
	}
	
	/**
	 * 添加商品
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sysCommodity:save")
	public R save(@RequestBody TbCommodityEntity tbCommodityEntity){
		sysCommodityService.save(tbCommodityEntity);
		
		return R.ok();
	}
	
	/**
	 * 修改商品属性
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sysCommodity:update")
	public R update(@RequestBody TbCommodityEntity tbCommodityEntity){
		sysCommodityService.update(tbCommodityEntity);

		return R.ok();
	}
	
	/**
	 * 删除商品(软删除)
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sysCommodity:delete")
	public R delete(@RequestBody Integer[] ids){
		sysCommodityService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
